package com.zx.Servlet;

import com.zx.Moudel.Infant;
import com.zx.Service.UserService;
import com.zx.Service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * com.zx.Servlet
 * 26832
 * zx
 * &#064;description: 婴儿疫苗预约
 * &#064;author: lxm
 * BookingServlet
 * &#064;create: 2024-06-22 09:05
 **/
@WebServlet("/booking")
public class BookingServlet extends HttpServlet {
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  doPost(req, resp);
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  //需要前端传递 //家长电话号、婴儿身份证号、婴儿姓名、婴儿性别、婴儿出生日期、婴儿所属社区、婴儿预约疫苗名称、预约时间\\
  //name分别为 pphone,idcard,uname,sex,borth,belong,vaccine
  req.setCharacterEncoding("utf-8");
  resp.setCharacterEncoding("utf-8");
  String pphone = req.getParameter("pphone");
  String idcard = req.getParameter("idcard");
  String uname = req.getParameter("uname");
  String sex = req.getParameter("sex");
  String borth = req.getParameter("borth");
  String belong = req.getParameter("belong");
  String vaccine = req.getParameter("vaccine");
  String date = req.getParameter("date");
  System.out.println("pphone:"+pphone+" idcard:"+idcard+" uname:"+uname+" sex:"+sex+" borth:"+borth+" belong:"+belong+" vaccine:"+vaccine+" date:"+date);
  Infant infant = new Infant();
  infant.setPphone(pphone);
  infant.setUname(uname);
  infant.setSex(sex);
  infant.setBorth(borth);
  infant.setBelong(belong);
  infant.setIdcard(idcard);
  infant.setData(date);

  UserService userService = new UserServiceImpl();
  //把idcard和vaccine存放到session中，方便后续查看预约结果
  HttpSession session = req.getSession();
  //根据idcard和belong查询该婴儿是否为本社区的婴儿
  //如果是：根据borth计算时间，查看是否能够接种预约的疫苗（borth,vaccine）
  //如果否：将婴儿基本信息插入到child表中，并且remark备注非本社区儿童，然后重复第二个步骤(Infant)
  //根据上面的操作，返回给前端预约结果success?faild
     try {
      if(userService.ExistQueue(infant)){
       if(userService.selectChild(belong,idcard)){
        //是本社区儿童
        if(userService.CalculationTime(borth,vaccine)){
         //可以预约
         if(userService.InsertQueue(idcard,vaccine,date)){
          session.setAttribute("idcard",idcard);
          session.setAttribute("vaccine",vaccine);
          resp.getWriter().write("本社区儿童，预约成功");
         }else{
          resp.getWriter().write("本社区儿童，预约失败");
         }
        }else{
         //不可以预约
         resp.getWriter().write("本社区儿童，未到接种疫苗时间");//未到打疫苗时间
        }
       }else {
        //不是本社区儿童
        if(userService.ExistChild(infant)){
         //执行业务
         if(userService.InsertChild(infant)){
          //插入成功
          if (userService.CalculationTime(borth,vaccine)){
           //可以预约
           if(userService.InsertQueue(idcard,vaccine,date)){
            session.setAttribute("idcard",idcard);
            session.setAttribute("vaccine",vaccine);
            resp.getWriter().write("非社区儿童，预约成功");
           }else{
            resp.getWriter().write("非社区儿童，未到接种疫苗时间");
           }
          }else{
           //不可以预约
           resp.getWriter().write("非本社区儿童，未到接种疫苗时间");
          }
         }else{
          //插入失败
          System.out.println("插入非本社区儿童失败");
          resp.getWriter().write("系统错误001");
         }
        }else{
         //停止更新child业务，直接更新queue业务(预约服务)
         System.out.println("已经存在数据");
         //判断能否进行打疫苗
         if (userService.CalculationTime(borth,vaccine)){
          //可以预约
          if(userService.InsertQueue(idcard,vaccine,date)){
           resp.getWriter().write("非社区儿童，预约成功");
          }else{
           resp.getWriter().write("非社区儿童，预约失败");
          }
         }else{
          //不可以预约
          resp.getWriter().write("非本社区儿童，不可以预约");
         }
        }
       }
      }else{
       resp.getWriter().write("已经预约过该疫苗");
      }

     } catch (SQLException e) {
         throw new RuntimeException(e);
     }
 }
}
